<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <template #header>
        <div class="clearfix">
          <span>班级管理</span>
        </div>
      </template>

      <div class="filter-item">
        <el-input
          v-model="listQuery.keyword"
          placeholder="班级名称"
          style="width: 200px;"
          prefix-icon="el-icon-search"
          clearable
        />
        <el-button @click="handleSearchList()" style="margin-left: 10px;">搜索</el-button>
        <el-button @click="listQuery = Object.assign({}, defaultListQuery); handleSearchList()">重置</el-button>
      </div>
      
      <div class="action-buttons">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleAddClass"
        >
          <el-icon><Plus /></el-icon>添加班级
        </el-button>
        <el-button @click="handleSearchList">
          <el-icon><Refresh /></el-icon>刷新
        </el-button>
      </div>

      <!-- 数据表格 -->
      <el-table
        ref="classTable"
        :data="list"
        style="width: 100%;"
        v-loading="listLoading"
        border
      >
        <el-table-column label="编号" width="100" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="班级名称" min-width="200">
          <template #default="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属专业" width="150" align="center">
          <template #default="scope">{{ scope.row.majorName }}</template>
        </el-table-column>
        <el-table-column label="所属院系" width="150" align="center">
          <template #default="scope">{{ scope.row.departmentName }}</template>
        </el-table-column>
        <el-table-column label="班主任" width="120" align="center">
          <template #default="scope">{{ scope.row.headTeacher }}</template>
        </el-table-column>
        <el-table-column label="学生人数" width="100" align="center">
          <template #default="scope">{{ scope.row.studentCount }}</template>
        </el-table-column>
        <el-table-column label="创建时间" width="160" align="center">
          <template #default="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
        <el-table-column label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.status | statusFilter">
              {{ scope.row.status | statusTextFilter }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template #default="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleUpdateClass(scope.$index, scope.row)"
            >
              <el-icon><Edit /></el-icon>编辑
            </el-button>
            <el-button
              size="mini"
              type="success"
              @click="handleViewStudents(scope.$index, scope.row)"
            >
              <el-icon><User /></el-icon>学生
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDeleteClass(scope.$index, scope.row)"
            >
              <el-icon><Delete /></el-icon>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
          :current-page.sync="listQuery.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="listQuery.size"
          :total="total"
        />
      </div>
    </el-card>

    <!-- 添加班级对话框 -->
    <el-dialog
      title="添加班级"
      :visible.sync="addDialogVisible"
      width="500px"
    >
      <!-- ... 添加班级表单内容保持不变 ... -->
    </el-dialog>

    <!-- 编辑班级对话框 -->
    <el-dialog
      title="编辑班级"
      :visible.sync="editDialogVisible"
      width="500px"
    >
      <!-- ... 编辑班级表单内容保持不变 ... -->
    </el-dialog>
  </div>
</template>

<script>
import { Plus, Edit, Refresh, Delete, User } from '@element-plus/icons-vue'
import { getClassPage, addClass as saveClass, updateClass, deleteClass } from '@/api/edu'

export default {
  name: 'ClassList',
  components: {
    Plus,
    Edit,
    Refresh,
    Delete,
    User
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        size: 10,
        keyword: ''
      },
      defaultListQuery: {
        page: 1,
        size: 10,
        keyword: ''
      },
      addDialogVisible: false,
      editDialogVisible: false,
      addForm: {
        name: '',
        majorId: '',
        headTeacher: '',
        status: 1
      },
      editForm: {
        id: '',
        name: '',
        majorId: '',
        headTeacher: '',
        status: 1
      },
      addRules: {
        name: [{ required: true, message: '请输入班级名称', trigger: 'blur' }],
        majorId: [{ required: true, message: '请选择专业', trigger: 'change' }],
        headTeacher: [{ required: true, message: '请输入班主任姓名', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      },
      editRules: {
        name: [{ required: true, message: '请输入班级名称', trigger: 'blur' }],
        majorId: [{ required: true, message: '请选择专业', trigger: 'change' }],
        headTeacher: [{ required: true, message: '请输入班主任姓名', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      },
      majorOptions: []
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'info',
        1: 'success'
      }
      return statusMap[status]
    },
    statusTextFilter(status) {
      const statusTextMap = {
        0: '禁用',
        1: '启用'
      }
      return statusTextMap[status]
    }
  },
  created() {
    this.getList()
    this.getMajorOptions()
  },
  methods: {
    getList() {
      this.listLoading = true
      getClassPage(this.listQuery).then(response => {
        this.list = response.records
        this.total = response.total
        this.listLoading = false
      })
    },
    getMajorOptions() {
      // 这里应该调用获取专业列表的API
      // 暂时使用模拟数据
      this.majorOptions = [
        { id: 1, name: '计算机科学与技术' },
        { id: 2, name: '软件工程' },
        { id: 3, name: '网络工程' }
      ]
    },
    handleSearchList() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleAddClass() {
      this.addDialogVisible = true
      this.addForm = {
        name: '',
        majorId: '',
        headTeacher: '',
        status: 1
      }
    },
    handleUpdateClass(index, row) {
      this.editDialogVisible = true
      this.editForm = Object.assign({}, row)
    },
    handleViewStudents(index, row) {
      // 跳转到学生管理页面，传递班级ID作为参数
      this.$router.push({ path: '/edu/students', query: { classId: row.id } })
    },
    submitAddForm() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          saveClass(this.addForm).then(response => {
            this.$message({
              message: '添加成功',
              type: 'success',
              duration: 2000
            })
            this.addDialogVisible = false
            this.getList()
          })
        }
      })
    },
    submitEditForm() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          updateClass(this.editForm).then(response => {
            this.$message({
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
            this.editDialogVisible = false
            this.getList()
          })
        }
      })
    },
    handleDeleteClass(index, row) {
      this.$confirm('确定要删除该班级吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteClass(row.id).then(response => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.filter-container {
  margin-bottom: 15px;
}

.filter-item {
  margin-bottom: 10px;
}

.action-buttons {
  display: flex;
  justify-content: flex-end;
  margin-top: -10px;
  margin-bottom: 10px;
}

.pagination-container {
  margin-top: 15px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  text-align: right;
}
</style>